package ZDloader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

/**
 * @author lanarenauser
 */
public class DbConnect {
    private static final String db = "jdbc:ucanaccess://build/main.mdb;showschema=true [ on PUBLIC]";
    private static Connection con;  
    private static Statement st;  
    private static ResultSet rs;  
    private static Gui gui = new Gui("e");
    private String gameTitle;
    private String sourceDir;
    private String destDir;
    
    public DbConnect(String game) throws Exception{
        connect(game);
    }

    private void connect(String game)throws Exception {
       try {  
	        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
	        con = DriverManager.getConnection(db,"","");  
	        //Open connection with database file
	        st = con.createStatement();   
	        //alert for success connection
	        //todo:remove
	        	JOptionPane.showMessageDialog(null,"Test Successful");  
	        
	        //Get Data for the given Game  
		    rs=st.executeQuery("select * from PUBLIC.APPLICATIONS WHERE PARAMETER = '" + game + "'");  
		    while(rs.next()) {  
		    	//Get the image path
		    	gameTitle = rs.getString("Image").toString();
		    	sourceDir = rs.getString("Source").toString();
		    	destDir   = rs.getString("Destination").toString();
		    } 
        }
        catch (Exception e)    {
              JOptionPane.showMessageDialog(null,"Failed to Connect to Database \n" +e,"Error Connection", JOptionPane.WARNING_MESSAGE);  
              System.exit(0);
        }
    }
    
    public String getGameTitle(){
		return gameTitle;
    	
    }
    public String getSourceTitle(){
    	return sourceDir;
    }
    public String getDestTitle(){
    	return destDir;
    }
    
    

}
